<?php
class Campaigns_tweets
{
	const DBTable = "tbl_campaigns_tweets";

	public $Tweet_id;	public $Campaign_id;	public $Tweet;	public $Tweet_created;	public $Tweet_username;	public $Tweet_signoff;	public $Status;	public $Is_deleted;	public $Createdate;	public $Updatedate;
	private $__mysql;

	public function __construct()
	{
		$this->Tweet_id = 0;		$this->Campaign_id = 0;		$this->Tweet = null;		$this->Tweet_created = date("Y-m-d H:i:s");		$this->Tweet_username = null;		$this->Tweet_signoff = null;		$this->Status = 0;		$this->Is_deleted = 0;		$this->Createdate = date("Y-m-d H:i:s");		$this->Updatedate = date("Y-m-d H:i:s");
		$this->__mysql = new MYSQL;
	}

	public function Save()
	{
		$sqlStr = "insert into ".self::DBTable."
				(campaign_id, tweet, tweet_created, tweet_username, tweet_signoff, status, createdate, updatedate)
				values
				(
				'".$this->Campaign_id."', 				'".mysql_real_escape_string($this->Tweet)."', 				'".$this->Tweet_created."', 				'".mysql_real_escape_string($this->Tweet_username)."', 				'".mysql_real_escape_string($this->Tweet_signoff)."', 				'".$this->Status."', 				'".$this->Createdate."', 				'".$this->Updatedate."'
				)";
			$this->__mysql->Exec_query($sqlStr);
			$this->Tweet_id = $this->__mysql->Get_insert_id();
			return $this->Tweet_id;
	}

	public function Update()
	{
		$sqlStr = "update ".self::DBTable." set
			campaign_id = '".$this->Campaign_id."', 			tweet = '".mysql_real_escape_string($this->Tweet)."', 			tweet_created = '".$this->Tweet_created."', 			tweet_username = '".mysql_real_escape_string($this->Tweet_username)."', 			tweet_signoff = '".mysql_real_escape_string($this->Tweet_signoff)."', 			status = '".$this->Status."', 			updatedate = '".$this->Updatedate."'
			where tweet_id = '".mysql_real_escape_string($this->Tweet_id)."'";
			$this->__mysql->Exec_query($sqlStr);
	}

	public function Quick_update($column = null, $value = null)
	{
		$sqlStr = "update ".self::DBTable." set
			".$column." = '".mysql_real_escape_string($value)."',
			 updatedate = '".$this->Updatedate."' 
			where tweet_id = '".mysql_real_escape_string($this->Tweet_id)."'";
			$this->__mysql->Exec_query($sqlStr);
	}

	public function List_all($limit = null, $offset = null)
	{
		$sqlStr = "select *
			from ".self::DBTable."
			where is_deleted = 0 
			order by tweet_created asc";
		if(isset($limit) && isset($offset)){
			$sqlStr .= " limit $offset, $limit";
		}
		//echo $sqlStr;
		$recordset = $this->__mysql->Exec_query($sqlStr);
		return $data_array = $this->__mysql->Get_data_array($recordset);
	}

	public function List_all_campaign_id($campaign_id = 0, $limit = null, $offset = null)
	{
		$sqlStr = "select *
			from ".self::DBTable."
			where is_deleted = 0 and 
			campaign_id = '".$campaign_id."'
			order by tweet_created asc";
		if(isset($limit) && isset($offset)){
			$sqlStr .= " limit $offset, $limit";
		}
		//echo $sqlStr;
		$recordset = $this->__mysql->Exec_query($sqlStr);
		return $data_array = $this->__mysql->Get_data_array_assoc($recordset);
	}

	public function List_all_selectlist()
	{
		$sqlStr = "select tweet_id, title
			from ".self::DBTable."
			where is_deleted = 0 
			order by title";
		//echo $sqlStr;
		$recordset = $this->__mysql->Exec_query($sqlStr);
		return $data_array = $this->__mysql->Get_data_array($recordset);
	}

	public function Delete($tweet_id = 0)
	{
		$sqlStr = "update ".self::DBTable." set is_deleted = 1 where tweet_id = '".mysql_real_escape_string($tweet_id)."'";
			$this->__mysql->Exec_query($sqlStr);

		$sqlStr = "optimize table ".self::DBTable."";
			$this->__mysql->Exec_query($sqlStr);
	}

	public function Select($tweet_id = 0)
	{
		$sqlStr = "select * from ".self::DBTable." where tweet_id = '".mysql_real_escape_string($tweet_id)."'";
		$recordset = $this->__mysql->Exec_query($sqlStr);

		if($this->__mysql->Get_num_rows($recordset) > 0)
		{
			$result = $this->__mysql->Get_data_assoc($recordset);
				$this->Tweet_id = $result['tweet_id'];				$this->Campaign_id = $result['campaign_id'];				$this->Tweet = $result['tweet'];				$this->Tweet_created = $result['tweet_created'];				$this->Tweet_username = $result['tweet_username'];				$this->Tweet_signoff = $result['tweet_signoff'];				$this->Status = $result['status'];				$this->Is_deleted = $result['is_deleted'];				$this->Createdate = $result['createdate'];				$this->Updatedate = $result['updatedate'];
			return true;
		}
		else
		{
			return false;
		}
	}

	public function Select_by($fieldname = null, $value = null)
	{
		if(!is_null($fieldname))
		{
			$sqlStr = "select * from ".self::DBTable." where ".$fieldname." = '".mysql_real_escape_string($value)."'";
			$recordset = $this->__mysql->Exec_query($sqlStr);

			if($this->__mysql->Get_num_rows($recordset) > 0)
			{
				$result = $this->__mysql->Get_data_assoc($recordset);
				$this->Tweet_id = $result['tweet_id'];				$this->Campaign_id = $result['campaign_id'];				$this->Tweet = $result['tweet'];				$this->Tweet_created = $result['tweet_created'];				$this->Tweet_username = $result['tweet_username'];				$this->Tweet_signoff = $result['tweet_signoff'];				$this->Status = $result['status'];				$this->Is_deleted = $result['is_deleted'];				$this->Createdate = $result['createdate'];				$this->Updatedate = $result['updatedate'];
				return true;
			}
			else
			{
				return false;
			}
		}
	}
}
?>